import {
    addBrandApi, 
    getBrandListApi, 
    changeIs_delApi, 
    recyclebinApi, 
    editBrandApi,
    delBrandAPI
    // getOneBrandApi,
    } from "../api/brandAPI"
import Vue from 'vue'
let that = Vue.prototype
const brandAbout = {
    namespaced: true,
    state:{
        // 品牌列表
        brandlist: [],
        // 品牌列表总条数
        total:0,
        // 品牌回收站
        brandlistBin: [],
        // 品牌回收站总条数
        bintotal: 0
    },
    getters:{

    },
    actions: {
        // 获取品牌列表
        async getBrandList(context, query){
            let data = await getBrandListApi(query)
            context.commit('get_brand_list', data)
        },
        // 添加品牌
        async addBrandData(context, query){
            let data = await addBrandApi(query)
            context.commit('add_brand_data', data)
        },
        // 删除品牌到回收站
        async changeIs_del(context, query){
            let {_id, is_del} = query
            is_del = 0
            await changeIs_delApi(_id, is_del)
            context.commit('change_is_del')
        },
        // 获取回收站品牌信息
        async getRecyclebin(context, query){
            let data = await recyclebinApi(query)
            context.commit('get_recycle_bin', data)
        },
        // 还原
        async restoreData(context, query){
            let {_id, is_del} = query
            is_del = 1
            await changeIs_delApi(_id, is_del)
            context.commit('restore_data')
        },

        // 修改
        async editBrandData(context, query){
            let {_id, brandname, brandsort, brandimg} = query
            let data = await editBrandApi(_id, {brandname, brandsort, brandimg})
            context.commit('edit_brand_data',  data)
        },

        // 彻底删除
        async delBrandData(context, query){
            let {data} = await delBrandAPI(query)
            context.commit('del_brand_data', data)
        }
    },
    mutations: {
        // 获取品牌列表映射getBrandList
        get_brand_list(state, data){
            if(data.status !== 200) return that.$message.error('获取品牌列表失败！')
            state.brandlist = data.data.data
            state.total = data.data.total
        },
        // 添加品牌映射addBrandData
        add_brand_data(state, data){
            if(data.status !==200) return that.$message.error('添加品牌失败！')
            that.$message.success('添加品牌成功！')
        },
        // 删除品牌到回收站映射changeIs_del
        change_is_del(state){
            that.$message.info('已将此品牌添加至回收站!')
        },
        // 获取回收站品牌信息映射getRecyclebin
        get_recycle_bin(state, data){
            if(data.status !== 200) return that.$message.error('获取品牌列表失败！')
            state.brandlistBin = data.data.data
            state.bintotal = data.data.total
        },
        // 还原映射restoreData
        restore_data(state){
            that.$message.success('已还原!')
        },

        // 修改品牌 映射editBrandData
        edit_brand_data(state, data){
            if(data.status !== 200) return that.$message.error('修改品牌失败！')
            that.$message.success('修改品牌成功！')
        },

        // 彻底删除
        del_brand_data(state, data){
            if (data.status !== 204) return that.$message.error('删除品牌失败！')
            that.$message.success('删除品牌成功！')
        }
    }
  }

  export default brandAbout